package com.java.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.java.entity.Passenger;
import com.java.utils.JDBCUtils;

public class PassengerDao {
	
	
	public int insert(Passenger p) {
		String sql = "insert into passenger(papersid,educationdegreeid,genderid,passengerlevelid,nationid,name,"
				+ "papersnumber,birthdate,licenceissuingauthorty,unitsoraddress,papersvaildity)  values(?,?,?,?,?,?,?,?,?,?,?)";
		List<Object> params = new ArrayList<>();
		params.add(p.getPapersid());
		params.add(p.getEducationdegreeid());
		params.add(p.getGenderid());
		params.add(p.getPassengerlevelid());
		params.add(p.getNationid());
		params.add(p.getName());
		params.add(p.getPapersnumber());
		params.add(p.getBirthdate());
		params.add(p.getLicenceissuingauthorty());
		params.add(p.getUnitsoraddress());
		params.add(p.getPapersvaildity());
		return JDBCUtils.excuteUpdate(sql, params);
	}
	
	/**
	 * @return
	 */
	public List<Passenger> queryAll(){
		String sql  = "select * from passenger";
		ResultSet rs = JDBCUtils.excuteQuery(sql, null);
		List<Passenger> list = new ArrayList<>();
		try {
			while(rs.next()) {
				Passenger p = new Passenger();
				p.setBirthdate(rs.getDate("birthdate"));
				p.setEducationdegreeid(rs.getInt("educationdegreeid"));
				p.setGenderid(rs.getInt("genderid"));
				p.setId(rs.getInt("id"));
				p.setLicenceissuingauthorty(rs.getString("licenceissuingauthorty"));
				p.setName(rs.getString("name"));
				p.setNationid(rs.getInt("nationid"));
				p.setPapersid(rs.getInt("papersid"));
				p.setPapersnumber(rs.getString("papersnumber"));
				p.setPapersvaildity(rs.getString("papersvaildity"));
				p.setPassengerlevelid(rs.getInt("passengerlevelid"));
				p.setUnitsoraddress(rs.getString("unitsoraddress"));
				list.add(p);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
